package com.iweb.dao;

import com.common.model.Page;
import com.iweb.pojo.Order;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    private Connection conn;
    private PreparedStatement pst;
    private ResultSet rs;

    public  OrderDaoImpl(Connection conn) {
        this.conn = conn;
    }

    @Override
    public List<Order> findAllOrder(Page page) throws Exception {
        List<Order> list = new ArrayList<>();
        String sql = "SELECT * FROM tb_order LIMIT ?,?";
        pst = conn.prepareCall(sql);

        pst.setInt(1,(page.getPage()-1)*page.getLimit());
        pst.setInt(2,page.getLimit());

        rs = pst.executeQuery();
        Order order = null;
        while (rs.next()){
            order = createOrder(rs);
            list.add(order);
        }

        return list;
    }

    @Override
    public List<Order> findNopayOrder(Page page) throws Exception {
        List<Order> list = new ArrayList<>();
        String sql = "SELECT * FROM tb_order where order_sta=? LIMIT ?,? ";
        pst = conn.prepareCall(sql);
        pst.setString(1,"待支付");
        pst.setInt(2,(page.getPage()-1)*page.getLimit());
        pst.setInt(3,page.getLimit());

        rs = pst.executeQuery();
        Order order = null;
        while (rs.next()){
            order = createOrder(rs);
            list.add(order);
        }

        return list;
    }

    @Override
    public List<Order> findPayedOrder(Page page) throws Exception {
        List<Order> list = new ArrayList<>();
        String sql = "SELECT * FROM tb_order where order_sta=? LIMIT ?,?";
        pst = conn.prepareCall(sql);

        pst.setString(1,"已支付");
        pst.setInt(2,(page.getPage()-1)*page.getLimit());
        pst.setInt(3,page.getLimit());

        rs = pst.executeQuery();
        Order order = null;
        while (rs.next()){
            order = createOrder(rs);
            list.add(order);
        }

        return list;
    }

    @Override
    public long selectCount() throws Exception {
        String sql = "select count(1) from tb_order";
        pst = conn.prepareStatement(sql);
        rs = pst.executeQuery();
        rs.next();
        return rs.getInt(1);
    }

    @Override
    public long selectCountNopay() throws Exception {
        String sql = "select count(1) from tb_order where order_sta=?";
        pst = conn.prepareStatement(sql);
        pst.setString(1,"待支付");
        rs = pst.executeQuery();
        rs.next();
        return rs.getInt(1);
    }

    @Override
    public long selectCountPayed() throws Exception {
        String sql = "select count(1) from tb_order where order_sta=?";
        pst = conn.prepareStatement(sql);
        pst.setString(1,"已支付");
        rs = pst.executeQuery();
        rs.next();
        return rs.getInt(1);
    }

    @Override
    public Order findById(Serializable order_id) throws Exception {
        Order order = null;
        String sql = "select * from tb_order where order_id= ?";
        pst = conn.prepareStatement(sql);
        pst.setObject(1,order_id);
        rs = pst.executeQuery();
        while (rs.next()){
            order = createOrder(rs);
        }
        return order;
    }

    @Override
    public void save(Order order) throws Exception {
        String sql = "insert into tb_order (order_id,order_cat,order_pic,order_sta,order_date) values (?,?,?,?,?)";

        pst = conn.prepareStatement(sql);
        pst.setString(1,order.getOrder_id());
        pst.setString(2,order.getOrder_cat());
        pst.setString(3,order.getOrder_pic());
        pst.setString(4,order.getOrder_sta());
        pst.setString(5,order.getOrder_date());
        pst.executeUpdate();
    }

    @Override
    public void updateById(Serializable id) throws Exception {
        String sql = "update  tb_order set order_sta =? where order_id = ?";
        pst = conn.prepareStatement(sql);
        pst.setString(1,"已支付");
        pst.setObject(2,id);
        pst.executeUpdate();
    }

    private Order createOrder(ResultSet rs) throws SQLException {
        Order order = new Order();
        order.setOrder_id(rs.getString("order_id"));
        order.setOrder_cat(rs.getString("order_cat"));
        order.setOrder_pic(rs.getString("order_pic"));
        order.setOrder_sta(rs.getString("order_sta"));
        order.setOrder_date(rs.getString("order_date"));
        return order;
    }

}
